﻿Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Collections.ObjectModel

Public Class TFSImport

    Private m_ItemWrapperCollection As ItemCollection

    Public Sub Import(ByRef ItemWrapperCollection As ItemCollection, ByVal teamProject As Project, ByVal Query As StoredQuery, ByVal TreeType As Node.TreeType)
        ' Clear current content.
        ItemWrapperCollection.Clear()
        Try
            ' Store localy for ease of access
            m_ItemWrapperCollection = ItemWrapperCollection
            ' Do import
            Dim NodeCol As NodeCollection = Nothing
            Select Case TreeType
                Case Node.TreeType.Area
                    NodeCol = teamProject.AreaRootNodes
                Case Node.TreeType.Iteration
                    NodeCol = teamProject.IterationRootNodes
            End Select

            Dim ProjectItem As ProjectItem = New ProjectItem(teamProject)
            m_ItemWrapperCollection.Add(ProjectItem)

            ImportNodes(ProjectItem, NodeCol)

            ImportWorkItems(ProjectItem, Query, teamProject, TreeType)

            ' Item added to the collection as the primary-Item.
            If m_ItemWrapperCollection.Count > 0 Then
                m_ItemWrapperCollection.Current = ProjectItem
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ImportNodes(ByVal Parent As ProjectItem, ByRef Nodes As NodeCollection)
        ' Add parent Nodes
        For Each n As Node In Nodes
            Dim newNodeItem As NodeItem = New NodeItem(n)
            m_ItemWrapperCollection.Add(newNodeItem)
            ImportChildNodes(n, newNodeItem)
        Next

        ' Add relationships
        For Each tn As Node In Nodes
            Dim Item As Item = m_ItemWrapperCollection.Find(CStr(tn.Uri.ToString))
            ' Add Top level nodes as children
            m_ItemWrapperCollection.AddChild(Parent, Item)
            For Each n As Node In Nodes
                If Not n Is tn Then

                    Dim Sibling As Item = m_ItemWrapperCollection.Find(CStr(n.Uri.ToString))
                    ' Add top level nodes as siblings
                    If Item Is Nothing OrElse Sibling Is Nothing Then
                        Throw New Exception("Could not find node")
                    End If
                    m_ItemWrapperCollection.AddSibling(Item, Sibling)
                End If
            Next
        Next

    End Sub

    Private Sub ImportChildNodes(ByRef Node As Node, ByVal iw As Item)
        For Each n As Node In Node.ChildNodes
            Dim newNodeItem As New NodeItem(n)
            m_ItemWrapperCollection.Add(newNodeItem)
            m_ItemWrapperCollection.AddChild(iw, newNodeItem)
            ImportChildNodes(n, newNodeItem)
        Next
    End Sub

    Private Sub ImportWorkItems(ByVal ProjectItem As ProjectItem, ByVal Query As StoredQuery, ByVal teamProject As Project, ByVal TreeType As Node.TreeType)
        If Not Query Is Nothing Then
            Dim strt As Long = Now.Ticks
            Dim QueryText As String = Query.QueryText.Replace("@project", String.Format("'{0}'", ProjectItem.Value.Name))
            Dim wis As WorkItemCollection = teamProject.Store.Query(QueryText)
            For Each WI As WorkItem In wis
                If WI.Project Is teamProject Then
                    Dim newWIItem As WIItem = New WIItem(WI)
                    'Dim WorkItemType As String = WI.Fields.Item("WorkItemType").Value

                    m_ItemWrapperCollection.Add(newWIItem)

                    ' Add realtionships
                    Dim NodeItem As Item = ProjectItem
                    Select Case TreeType
                        Case Node.TreeType.Area
                            If Not WI.AreaPath = ProjectItem.Name Then
                                Dim Path As String = WI.AreaPath.Substring(teamProject.Name.Length, WI.AreaPath.Length - teamProject.Name.Length)
                                Dim node = teamProject.FindNodeInSubTree(Path, Microsoft.TeamFoundation.WorkItemTracking.Client.Node.TreeType.Area)
                                NodeItem = m_ItemWrapperCollection.Find(node.Uri.ToString)
                            End If
                        Case Node.TreeType.Iteration
                            If Not WI.IterationPath = ProjectItem.Name Then
                                Dim Path As String = WI.IterationPath.Substring(teamProject.Name.Length, WI.IterationPath.Length - teamProject.Name.Length)
                                Dim node = teamProject.FindNodeInSubTree(Path, Microsoft.TeamFoundation.WorkItemTracking.Client.Node.TreeType.Iteration)
                                NodeItem = m_ItemWrapperCollection.Find(node.Uri.ToString)
                            End If
                    End Select
                    m_ItemWrapperCollection.AddWorkItem(NodeItem, newWIItem)
                    ' TODO: Add Inter Work Item Relationships
                End If
                Debug.Print("ImportWorkItems took {0} seconds", New TimeSpan(Now.Ticks - strt).TotalSeconds)
            Next
            '
#If DEBUG Then
            For Each WorkItemType As WorkItemType In teamProject.WorkItemTypes
                Debug.Print("WorkItemType:{0}", WorkItemType.Name)
                Debug.Print("{0}", WorkItemType.Description)
                Debug.Print("{0}{1}{0}{2}", vbTab, "Name", "ReferenceName")
                For Each fd As FieldDefinition In WorkItemType.FieldDefinitions
                    Debug.Print("{0}{1}{0}{2}", vbTab, fd.Name, fd.ReferenceName)
                Next
            Next
#End If
        End If

    End Sub

End Class
